package com.inhim.pjhim.dowloadfile.adapter.provider;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chaychan.adapter.BaseItemProvider;
import com.inhim.pjhim.R;
import com.inhim.pjhim.dowloadfile.adapter.DownloadListAdapter;
import com.inhim.pjhim.dowloadfile.download.DownloadInfo;
import com.inhim.pjhim.dowloadfile.download.limit.DownloadLimitManager;
import com.inhim.pjhim.dowloadfile.utils.FileUtil;
import com.inhim.pjhim.listener.ItemClickListener;
import com.inhim.pjhim.utils.ImageLoaderUtils;

import java.util.Map;

/**
 * @author ChayChan
 * @description: 章节标题
 * @date 2018/3/22  14:48
 */

public class DownloadingProvider extends BaseItemProvider<DownloadInfo, BaseViewHolder> {

    private Context context;
    private boolean isCheck;
    private Map<Integer, Boolean> mDeviceHeaderMap;
    private NotifyDataSetChanged mNotifyDataSetChanged;

    public DownloadingProvider(Context context) {
        this.context = context;
    }

    public void setCheck(boolean isCheck, Map<Integer, Boolean> mDeviceHeaderMap) {
        this.isCheck = isCheck;
        this.mDeviceHeaderMap = mDeviceHeaderMap;
    }

    @Override
    public void convert(BaseViewHolder helper, final DownloadInfo data, int i) {
        if (data == null) {
            //如果没有，则直接跳过
            return;
        }
        try {
            //处理相关业务逻辑
            TextView tv_size = helper.getView(R.id.tv_size);
            TextView tv_status = helper.getView(R.id.tv_status);
            TextView tv_name = helper.getView(R.id.tv_name);
            ImageView imageview1 = helper.getView(R.id.imageview1);
            //ImageView imageview2 = helper.getView(R.id.imageview2);
            CheckBox checkbox = helper.getView(R.id.checkbox);
            ProgressBar progressBar = helper.getView(R.id.progressBar);
            ConstraintLayout constran=helper.getView(R.id.constran);
            if (data.getProgress() != 0 && data.getTotal() != 0) {
                float d = (float) data.getProgress() * (float) progressBar.getMax() / (float) data.getTotal();
                progressBar.setProgress((int) d);
                DownloadInfo downloadInfo = new DownloadInfo();
                //直接更新id为1的记录
                downloadInfo.setProgress(data.getProgress());
                downloadInfo.setTotal(data.getTotal());
                downloadInfo.updateAll("url = ?", data.getUrl());
            } else {
                progressBar.setProgress(0);
            }
            tv_name.setText(data.getTitle());
            tv_size.setText(FileUtil
                    .formatFileSize(data.getTotal()));
            if (data.getProgressText() != null && !data.getProgressText().equals("")) {
                if(data.getProgressText().equals("0")){
                    tv_status.setText("已暂停");
                }else{
                    tv_status.setText(data.getProgressText());
                }
            } else {
                tv_status.setText("已暂停");
            }
            ImageLoaderUtils.setImage(data.getCover(), imageview1);
            //imageview2.setImageResource(R.mipmap.icon_video);
            if (isCheck) {
                checkbox.setVisibility(View.VISIBLE);
            } else {
                checkbox.setVisibility(View.GONE);
            }
            if (mDeviceHeaderMap!=null && mDeviceHeaderMap.get(i)) {
                checkbox.setChecked(true);
            } else {
                checkbox.setChecked(false);
            }

            checkbox.setOnClickListener(v-> {
                if(mNotifyDataSetChanged!=null){
                    mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),i);
                }
            });

            if (DownloadLimitManager.getInstance().getWaitUrl(data.getUrl())) {
                //imageview2.setImageResource(R.mipmap.icon_download);
                constran.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(checkbox.getVisibility()==View.VISIBLE){
                            if(mNotifyDataSetChanged!=null){
                                checkbox.setChecked(!checkbox.isChecked());
                                mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),i);
                            }
                        }else{
                            DownloadLimitManager.getInstance().download(data.getUrl());
                            DownloadLimitManager.getInstance().setTitle(data.getTitle());
                        }
                    }
                });
            } else if (DownloadInfo.DOWNLOAD_CANCEL.equals(data.getDownloadStatus())) {
                //imageview2.setImageResource(R.mipmap.icon_download);
                constran.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(checkbox.getVisibility()==View.VISIBLE){
                            if(mNotifyDataSetChanged!=null){
                                checkbox.setChecked(!checkbox.isChecked());
                                mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),i);
                            }
                        }else{
                            DownloadLimitManager.getInstance().download(data.getUrl());
                            DownloadLimitManager.getInstance().setTitle(data.getTitle());
                        }
                    }
                });
            } else if (DownloadInfo.DOWNLOAD_PAUSE.equals(data.getDownloadStatus())) {
                //imageview2.setImageResource(R.mipmap.icon_download);
                constran.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(checkbox.getVisibility()==View.VISIBLE){
                            if(mNotifyDataSetChanged!=null){
                                checkbox.setChecked(!checkbox.isChecked());
                                mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),i);
                            }
                        }else{
                            DownloadLimitManager.getInstance().download(data.getUrl());
                            DownloadLimitManager.getInstance().setTitle(data.getTitle());
                        }
                    }
                });
            } else if (DownloadInfo.DOWNLOAD_ERROR.equals(data.getDownloadStatus())) {
                DownloadLimitManager.getInstance().download(data.getUrl());
                DownloadLimitManager.getInstance().setTitle(data.getTitle());
            } else if (DownloadInfo.DOWNLOAD_OVER.equals(data.getDownloadStatus())) {
            /*holder.main_progress.setProgress(holder.main_progress.getMax());
            holder.main_btn_down.setText("完成");*/
                //List<DownloadInfo> infos = getDownloadListData();
                    /*mdata.clear();
                    mdata.addAll(infos);
                    Intent intent = new Intent();
                    intent.setAction("the.search.data.dowload");
                    mContext.sendBroadcast(intent);
                    notifyDataSetChanged();*/
                if (mNotifyDataSetChanged != null) {
                    mNotifyDataSetChanged.notifyChanged();
                }
            } else if (DownloadInfo.DOWNLOAD.equals(data.getDownloadStatus())) {
                if(data.getProgress()==0){
                    tv_status.setText("已暂停");
                }else{
                    tv_status.setText(FileUtil.formatFileSize(data.getProgress()));
                }
                tv_size.setText(FileUtil
                        .formatFileSize(data.getTotal()));
                constran.setOnClickListener(view -> {
                    if(checkbox.getVisibility()==View.VISIBLE){
                        if(mNotifyDataSetChanged!=null){
                            checkbox.setChecked(!checkbox.isChecked());
                            mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),i);
                        }
                    }else{
                        DownloadLimitManager.getInstance().pauseDownload(data.getUrl());
                        DownloadLimitManager.getInstance().setTitle(data.getTitle());
                    }
                });
                //imageview2.setImageResource(R.mipmap.icon_puse);
            } else {
                //等待中
                if (data.getTotal() == 0) {

                    ImageLoaderUtils.setImage(data.getCover(), imageview1);
                    if(data.getProgress()==0){
                        tv_status.setText("已暂停");
                    }else {
                        tv_status.setText(FileUtil.formatFileSize(data.getProgress()));
                    }
                    tv_size.setText(FileUtil
                            .formatFileSize(data.getTotal()));
                    constran.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(checkbox.getVisibility()==View.VISIBLE){
                                if(mNotifyDataSetChanged!=null){
                                    checkbox.setChecked(!checkbox.isChecked());
                                    mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),i);
                                }
                            }else{
                                DownloadLimitManager.getInstance().download(data.getUrl());
                                DownloadLimitManager.getInstance().setTitle(data.getTitle());
                            }
                        }
                    });
                    //tv_name.setText(data.getTitle());
                } else {
                    ImageLoaderUtils.setImage(data.getCover(), imageview1);
                    tv_status.setText(FileUtil.formatFileSize(data.getProgress()));
                    tv_size.setText(FileUtil
                            .formatFileSize(data.getTotal()));
                    constran.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(checkbox.getVisibility()==View.VISIBLE){
                                if(mNotifyDataSetChanged!=null){
                                    checkbox.setChecked(!checkbox.isChecked());
                                    mNotifyDataSetChanged.checkOnClick(checkbox.isChecked(),i);
                                }
                            }else{
                                DownloadLimitManager.getInstance().download(data.getUrl());
                                DownloadLimitManager.getInstance().setTitle(data.getTitle());
                            }
                        }
                    });
                    //imageview2.setImageResource(R.mipmap.icon_download);
                    //tv_name.setText(data.getTitle());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public interface NotifyDataSetChanged {
        void notifyChanged();
        void checkOnClick(boolean isCheck,int position);
    }

    public void setNotifyDataSetChanged(NotifyDataSetChanged notifyDataSetChanged) {
        this.mNotifyDataSetChanged = notifyDataSetChanged;
    }

    @Override
    public int viewType() {
        return DownloadListAdapter.DOWNLOADING;
    }

    @Override
    public int layout() {
        return R.layout.item_downloading;
    }
}
